package edu.njust.cma.service.impl;

import edu.njust.cma.DTO.Excel04;
import edu.njust.cma.DTO.Excel05;
import edu.njust.cma.mapper.EquipmentMapper;
import edu.njust.cma.pojo.Equipment;
import edu.njust.cma.service.EquipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class EquipServiceImpl implements EquipService {
    @Autowired
    private EquipmentMapper equipMapper;

    //1.查询设备列表
    @Override
    public List<Equipment> equipList() {
        return equipMapper.equipList();
    }

    //2.添加设备
    @Override
    public void addEquip(Equipment equipment) {
        equipMapper.addEquip(equipment);
    }

    //3.删除设备
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteEquip(int id) {
        //先检查subTask表中是否有正在使用该设备
        List<Integer> subTaskList = equipMapper.getSubTaskEquipmentList(id);
        if(subTaskList.size() > 0) {
            throw new RuntimeException("该设备正在被使用，无法删除");
        }
        else {
            equipMapper.deleteEquip(id);
        }

    }

    //4.修改设备信息
    @Override
    public void updateEquip(Equipment equipment) {
        equipMapper.updateEquip(equipment);
    }

    //5.通过关键字查询设备
    @Override
    public List<Equipment> searchEquip(String keyword) {
        List<Equipment> equipList = equipMapper.searchEquip(keyword);
        return equipList;
    }

    //生成Excel04, 获取Excel04的DTO类
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Excel04> equipListExcel04(int projectId) {
        List<Integer> standardIdList = equipMapper.getStandardIdListByProjectId(projectId);
        List<Integer> smallProjectIdList = equipMapper.getSmallProjectIdListByProjectId(projectId);
        //初始化excel04List
        List<Excel04> excel04List = new ArrayList<>();

        for(int smallProjectId : smallProjectIdList) {
            //将获得的standardIdList中的每一个standardId对应的Excel04对象加入excel04List
            for(int standardId : standardIdList) {

                String majorCategoryName = equipMapper.getMajorCategoryNameByStandardId(standardId);
                String minorCategoryName = equipMapper.getMinorCategoryNameByStandardId(standardId);

                List<String> taskNames = equipMapper.getTaskNameBySmallProjectId(smallProjectId);

                String standardName = equipMapper.getStandardNameByStandardId(standardId);
                String standardNumber = equipMapper.getStandardNumberByStandardId(standardId);
                for(String taskName:taskNames){
                    Excel04 excel04 = new Excel04();
                    //将Excel04对象属性赋值
                    excel04.setMajorCategoryName(majorCategoryName);
                    excel04.setMinorCategoryName(minorCategoryName);
                    excel04.setTaskName(taskName);
                    excel04.setStandardName(standardName);
                    excel04.setStandardNumber(standardNumber);
                    //将Excel04对象加入excel04List
                    excel04List.add(excel04);
                }
            }
        }


        return excel04List;
    }

    //生成Excel05, 获取Excel05的DTO类
    @Transactional(rollbackFor = Exception.class)
    @Override
    public List<Excel05> equipListExcel05(int projectId) {
        List<Integer> standardIdList = equipMapper.getStandardIdListByProjectId(projectId);
        List<Integer> smallProjectIdList = equipMapper.getSmallProjectIdListByProjectId(projectId);

        //去除重复的smallProjectId
        List<Integer> smallProjectIdList2 = new ArrayList<>();
        for(int smallProjectId : smallProjectIdList) {
            if(!smallProjectIdList2.contains(smallProjectId)) {
                smallProjectIdList2.add(smallProjectId);
            }
        }
        smallProjectIdList = smallProjectIdList2;

        //获取TaskIdList
        List<Integer> taskIdList = new ArrayList<>();
        //根据smallProjectId获取taskId
        for (Integer smallProjectId : smallProjectIdList) {
            List<Integer>taskIdList1 = equipMapper.getTaskIdListBySmallProjectId(smallProjectId);
            taskIdList.addAll(taskIdList1);
        }
        //去除重复的taskId
        List<Integer> taskIdList2 = new ArrayList<>();
        for(int taskId : taskIdList) {
            if(!taskIdList2.contains(taskId)) {
                taskIdList2.add(taskId);
            }
        }
        taskIdList = taskIdList2;

        //初始化excel05List
        List<Excel05> excel05List = new ArrayList<>();

        for(int smallProjectId : smallProjectIdList) {
            //将获得的standardIdList中的每一个standardId对应的Excel04对象加入excel04List
            for(int standardId : standardIdList) {
                for(int taskId : taskIdList){
                    for(int equipmentId: equipMapper.getEquipmentIdListByTaskId(taskId)){

                        String minorCategoryName = equipMapper.getMinorCategoryNameByStandardId(standardId);
                        List<String> taskNames = equipMapper.getTaskNameBySmallProjectId(smallProjectId);
                        String standardName = equipMapper.getStandardNameByStandardId(standardId);
                        String standardNumber = equipMapper.getStandardNumberByStandardId(standardId);
                        //以下是设备部分
                        String equipmentNumber = equipMapper.getEquipmentNumberByEquipmentId(equipmentId);
                        String equipmentName = equipMapper.getEquipmentNameByEquipmentId(equipmentId);
                        String expiryTime = equipMapper.getExpiryTimeByEquipmentId(equipmentId);
                        String measuringRange = equipMapper.getMeasuringRangeByEquipmentId(equipmentId);
                        String description = equipMapper.getDescriptionByEquipmentId(equipmentId);
                        int traceway = equipMapper.getTraceWayByEquipmentId(equipmentId);
                        for(String taskName :taskNames) {
                            Excel05 excel05 = new Excel05();
                            //将Excel05对象属性赋值
                            excel05.setMinorCategoryName(minorCategoryName);
                            excel05.setTaskName(taskName);
                            excel05.setStandardName(standardName);
                            excel05.setStandardNumber(standardNumber);
                            excel05.setEquipmentNumber(equipmentNumber);
                            excel05.setEquipmentName(equipmentName);
                            excel05.setExpiryTime(expiryTime);
                            excel05.setMeasuringRange(measuringRange);
                            excel05.setDescription(description);
                            excel05.setTraceway(traceway);
                            //将Excel05对象加入excel05List
                            excel05List.add(excel05);
                        }
                    }

                }

            }
        }

        return excel05List;
    }
}
